#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include "client.h"

////



char getFirst()
{
  char ch;

  ch = getchar();
  while ( getchar() != '\n') //skip the rest of the line
    continue;

  return ch;
}

void client()
{
  char ch;
  int sFile, sMsge;

  while (1)
  {
    printf("Please enter the letter of your choice:\n"
        "a. Connect to a host\n"
        "q. Quit\n");

    ch = getFirst();
    while ( (ch != 'a') && (ch != 'q') )
    {
      printf("Please respond with \'a\' or \'q\'.\n");
      ch = getFirst();
    }

    if (ch == 'a')
    {
      if (clientConn(&sFile, &sMsge) == -1)
        continue;
      else
      {
        clientFunctions(sFile, sMsge);
      }
    }
    else if (ch == 'q') 
    {
      return;
    }
  }
}

int clientConn(int *sFile, int *sMsge)
{
  struct sockaddr_in addr;
  char ipAddr[IPADDRLEN];

  printf("Please enter the IP address you want to connect to:\n");
  fgets(ipAddr, IPADDRLEN, stdin);

  printf("%s", ipAddr);

  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(SERVERPORT);
  addr.sin_addr.s_addr = inet_addr(ipAddr);

  if ((*sFile = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
  {
    printf("socket() error!\n"); 
    return -1;
  }

  if (connect(*sFile, (struct sockaddr *) &addr, sizeof(addr))) 
  {
    printf("connect() error!\n"); 
    close(*sFile);
    return -1;
  }

  if ((*sMsge = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
  {
    printf("socket() error!\n"); 
    close(*sFile);
    return -1;
  }

  if (connect(*sMsge, (struct sockaddr *) &addr, sizeof(addr))) 
  {
    printf("connect() error!\n"); 
    close(*sFile);
    close(*sMsge);
    return -1;
  }

  return 0;
}


void clientFunctions(int sFile, int sMsge)
{
  char ch;

  while(1)
  {
    printf("What do you want to do?\n"
        "a. Send Message\n"
        "b. Send a file\n"
        "c. Close connection\n");

    ch = getFirst();
    while ( (ch != 'a') && (ch != 'b') && (ch != 'c') )
    {
      printf("Please respond with \'a\', \'b\' or \'c\'.\n");
      ch = getFirst();
    }

    if (ch == 'a')
    {
      sendMessage(sMsge);
    }
    else if (ch == 'b')
    {
      sendFile(sFile);
    }
    else
    {
      break;
    }
  }
}

void sendMessage(int s)
{
  char buf[BUFSIZ];

  buf[BUFSIZ] = 0;

  /* tell server that a message is to be sent */
  send(s, "MSGE", 4, 0);

  printf("You can input message now. Press Crtl+D to exit.\n");

  while (fgets(buf, BUFSIZ, stdin) != NULL) 
  {
    if (write(s, buf, strlen(buf)) == 0) 
    {
      printf("write() error!\n");
      return;
    }
  }
}

void sendFile(int s)
{
  FILE *f;
  char ch, fileName[256], fileBuf[BUFSIZ];
  int count = 0;

  /* tell server that a file is to be sent */
  if ( send(s, "FILE", 4, 0) != 4)
  {
    printf("the TYPE \'FILE\' send fail\n");
  }


  printf("Please input the path of the file:\n");

  gets(fileName);

  if ( (f = fopen(fileName, "r") ) == NULL)
  {
    printf("Can't open %s!\n", fileName);
    return;
  }

  while ((ch = getc(f)) != EOF)
  {
    fileBuf[count] = ch;
    count ++;
  }
  //fileBuf[count] = 0;
  count --;

  if (write(s, fileBuf, count) == 0) 
  {
    printf("Send file error!\n");
    return;
  }

  printf("File sent!\n");
}

/* main test */
int main()
{
  client();
  return 0;
}


